/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uy.edu.ort.uruguayviajes.web;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import uy.edu.ort.uruguayviajes.util.MensajesUtil;

/**
 *
 * @author magui
 */

@FacesValidator(value = "emailValidator")
public class EmailValidator implements Validator{

    
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

        String miMsg = MensajesUtil.obtenerMensaje("EMAIL_INCORRECTO", null);

        if (null != value) {
            String email = (String) value;
            Pattern p = Pattern.compile("[a-zA-Z0-9]+[.[a-zA-Z0-9_-]+]*@[a-z0-9][\\w\\.-]*[a-z0-9]\\.[a-z][a-z\\.]*[a-z]$");//me gusta esta
            Matcher m = p.matcher(email);
            if (!m.matches()) {
                FacesMessage msg = new FacesMessage(miMsg);
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(msg);
            }
        } else {
            FacesMessage msg = new FacesMessage(miMsg);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }



    
}
